//
//  ZXGetPhoto.m
//  BLACKSTONE
//
//  Created by fly-zhang on 16/11/2
//  Copyright (c) 2016年 IMT. All rights reserved.
//

#import "ZXGetPhoto.h"
//#import "ZLPhotoAssets.h"
//#import "ZLPhotoPickerAssetsViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import <AVFoundation/AVCaptureDevice.h>
#import <AVFoundation/AVMediaFormat.h>
#import <Photos/Photos.h>
#import "MGConstFile.h"
#import "UIColor+Customer.h"

@interface ZXGetPhoto ()<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic,weak) UIViewController <ZXGetPhotoDelegate> *delegate;
@end

@implementation ZXGetPhoto

+(ZXGetPhoto *)getPhotoManageWithDelegate:(UIViewController<ZXGetPhotoDelegate> *)delegate
{
    ZXGetPhoto *photo = [ZXGetPhoto new];
    [photo showWithDelegate:delegate];
    return photo;
}

- (void)showWithDelegate:(UIViewController<ZXGetPhotoDelegate> *)delegate
{
    if (delegate)
    {
        [delegate.view endEditing:YES];
        
        self.delegate = delegate;
        
        NSString *title_idx_0 = @"拍照";
        NSString *title_idx_1 =@"从相册选择";
        NSString *cancel_idx =@"取消";
    //  使用黑魔法
        title_idx_0.tintColor=[UIColor add_colorWithRGBHexString:@"333333"];
        title_idx_1.tintColor=[UIColor add_colorWithRGBHexString:@"333333"];
        cancel_idx.tintColor=[UIColor add_colorWithRGBHexString:@"999999"];

        UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:cancel_idx destructiveButtonTitle:nil otherButtonTitles:title_idx_0,title_idx_1, nil];
        
        [action showInView:self.delegate.view];
       
    }
}

#pragma mark - Add Picture
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    _isAllowEdit = [Util isAssetsLibraryAccessShowTips:NO] ? _isAllowEdit : NO;

    if (buttonIndex == 0)
    {
        if ([Util isCameraAccessShowTips:YES]) {
            [self addCarema];
        }
    }
    else if (buttonIndex == 1)
    {
        if ([Util isAssetsLibraryAccessShowTips:YES])
        {
//            if (_is_morePhoto) {
//                [self photoSelecte];
//            }else{
                [self openPicLibrary];
//            }
        }
    }
}

-(void)addCarema{
    
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = self.isAllowEdit;
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self.delegate presentViewController:picker animated:YES completion:nil];
}

-(void)openPicLibrary{
    
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = self.isAllowEdit;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self.delegate presentViewController:picker animated:YES completion:nil];
        
//        [UIFactory statusWithStyle:UIStatusBarStyleDefault];
    }
}

//- (void)photoSelecte{
//    
//    ZLPhotoPickerViewController *pickerVc = [[ZLPhotoPickerViewController alloc] init];
//    pickerVc.minCount =_remaincount;
//    pickerVc.status = PickerViewShowStatusCameraRoll;
//    
//    pickerVc.callBack = ^(NSArray *status){
//        if (_selectArrayPhoto) {
//            _selectArrayPhoto(status);
//        }
//    };
//    
//    [pickerVc show];
//}

#pragma mark -- UIImagePickerController delegate --
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    
//    if (picker.viewControllers.count == 1) {
//        [UIFactory statusWithStyle:UIStatusBarStyleDefault];
//    }else{
//        [UIFactory statusWithStyle:UIStatusBarStyleBlackOpaque];
//    }

    if (_is_morePhoto) {
//        [picker dismissViewControllerAnimated:YES completion:nil];
//        UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];
//        NSData *tempData=UIImageJPEGRepresentation(image, 0.5f);
//        image=[UIImage imageWithData:tempData];
//        
//        ZLPhotoAssets *photo=[[ZLPhotoAssets alloc] init];
//        photo.image=image;
//        NSArray *arr=@[photo];
//        
//        if (_selectArrayPhoto) {
//            _selectArrayPhoto(arr);
//        }
//        return;
    }
    
    NSString *imgPicker_key = nil;
    if (_isAllowEdit) {
        imgPicker_key = UIImagePickerControllerEditedImage;
    }else{
        imgPicker_key = UIImagePickerControllerOriginalImage;
    }
    UIImage *m_Image = [info objectForKey:imgPicker_key];
    if (_delegate && [_delegate respondsToSelector:@selector(wFPhoto:sendPicture:)])
    {
        [self.delegate wFPhoto:self sendPicture:m_Image];
    }
    
    [self.delegate dismissViewControllerAnimated:YES completion:nil];
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
//    if (picker.viewControllers.count == 1) {
//        [UIFactory statusWithStyle:UIStatusBarStyleDefault];
//    }else{
//        [UIFactory statusWithStyle:UIStatusBarStyleBlackOpaque];
//    }

    if (_delegate && [_delegate respondsToSelector:@selector(wFPhotoDidCancel:)]) {
        [self.delegate wFPhotoDidCancel:self];
    }
    
    [self.delegate dismissViewControllerAnimated:YES completion:nil];
}
// dosomething
#pragma clang diagnostic pop

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
    for (UIView *subViwe in actionSheet.subviews) {
        if ([subViwe isKindOfClass:[UIButton class]]) {
            UIButton *button = (UIButton*)subViwe;
            [button setTitleColor:[UIColor add_colorWithRGBHexString:@"333333"] forState:UIControlStateNormal];
        }
    }
}


@end
